#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>


#define BUF_SIZE 30
void error_handling(char *msg);

int main(int argc, char *argv[])
{
    int sock;
    char msg[BUF_SIZE];
    struct sockaddr_in my_addr;
    struct sockaddr_in your_addr;
    socklen_t adr_sz;
    int str_len;
    int i;

    if (2 != argc) {
        printf("Usage: %s <port>\n", argv[0]);
        exit(1);
    }

    sock = socket(PF_INET, SOCK_DGRAM, 0);
    if (-1 == sock) {
        error_handling("socket() error");
    }

    memset(&my_addr, 0, sizeof(my_addr));
    my_addr.sin_family = AF_INET;
    my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    my_addr.sin_port =  htons(atoi(argv[1]));

    if (-1 == bind(sock, (struct sockaddr *) &my_addr, sizeof(my_addr))) {
        error_handling("bind() error");
    }

    for (i = 0;i < 3; i++) {
        sleep(5);
        adr_sz = sizeof(your_addr);
        str_len = recvfrom(sock, msg, BUF_SIZE, 0,
            (struct sockaddr *)&your_addr, &adr_sz);

        printf("Message %d: %s \n", i + 1, msg);
    }
    close(sock);


    return 0;
}
void error_handling(char *msg)
{
    fputs(msg, stderr);
    fputc('\n', stderr);
    exit(1);
}

